package com.demo.java.OD251_300.OD252;

import java.util.Scanner;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【机智的外卖员】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146029086
 */
public class OdMain {
    // 计算以2为底的对数
    private static int log2(int n) {
        int log = 0;
        while (n != 1) {
            n = n / 2;
            log++;
        }
        return log;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt(); // 当前所在位置
        int M = in.nextInt(); // 目标位置

        // 无效输入情况
        if (N <= 0 || M <= 0) {
            System.out.println("Invalid input");
            return;
        }

        // 如果当前位置和目标位置相同，直接输出0
        if (N >= M) {
            System.out.println(0);
            return;
        }

        int a = N, b = M;
        int left = log2(a); // 当前层数的log2值
        int right = log2(b); // 目标层数的log2值

        // 计算最短路径，考虑四种不同的情况进行优化
        int min = right - left + (N - (1 << left)) + M - (1 << right);
        min = Math.min(min, right + 1 - left + (N - (1 << left)) + ((1 << (right + 1)) - M));
        min = Math.min(min, right - left - 1 + (1 << (left + 1)) - N + M - (1 << right));
        min = Math.min(min, right - left + (1 << (left + 1)) - N + ((1 << (right + 1)) - M));

        // 输出最短路径
        System.out.println(min);
    }
}